CMAKE_MINIMUM_REQUIRED(VERSION 3.5 FATAL_ERROR)

# parse the full version number from jversion.h and include in JPEG_FULL_VERSION
FILE(READ ${LIBJPEG_SOURCE_DIR}/jversion.h _jversion_h_contents)

STRING(REGEX REPLACE ".*#define[ \t]+JVERSION[ \t]+\"([0-9]+[a-z]+).+\".*" "\\1" JPEG_FULL_VERSION ${_jversion_h_contents})
STRING(REGEX REPLACE "([0-9]+)[a-z]+" "\\1" JPEG_MAJOR_VERSION ${JPEG_FULL_VERSION})
STRING(REGEX REPLACE "[0-9]+([a-z]+)" "\\1" JPEG_MINOR_VERSION ${JPEG_FULL_VERSION})

# Convert minor version letter to number
IF ("${JPEG_MINOR_VERSION}" MATCHES [aA])
  SET(JPEG_MINOR_VERSION 0)
ELSEIF("${JPEG_MINOR_VERSION}" MATCHES [bB])
  SET(JPEG_MINOR_VERSION 1)
ELSEIF("${JPEG_MINOR_VERSION}" MATCHES [cC])
  SET(JPEG_MINOR_VERSION 2)
ELSEIF("${JPEG_MINOR_VERSION}" MATCHES [dD])
  SET(JPEG_MINOR_VERSION 3)
ELSEIF("${JPEG_MINOR_VERSION}" MATCHES [eE])
  SET(JPEG_MINOR_VERSION 4)
ELSEIF("${JPEG_MINOR_VERSION}" MATCHES [fF])
  SET(JPEG_MINOR_VERSION 5)
ELSEIF("${JPEG_MINOR_VERSION}" MATCHES [gG])
  SET(JPEG_MINOR_VERSION 6)
ELSEIF("${JPEG_MINOR_VERSION}" MATCHES [hH])
  SET(JPEG_MINOR_VERSION 7)
ELSE()
  MESSAGE(FATAL_ERROR "Minor JPEG version exceeds version H")
ENDIF()

PROJECT(jpeg VERSION ${JPEG_MAJOR_VERSION}.${JPEG_MINOR_VERSION} LANGUAGES C)

# Use GNUInstallDirs to install libraries into correct
# locations on all platforms.
INCLUDE(GNUInstallDirs)

IF(MSVC)
  SET(jconfig "${LIBJPEG_SOURCE_DIR}/jconfig.vc")
ENDIF()

IF (MINGW)
  SET(jconfig "${LIBJPEG_SOURCE_DIR}/jconfig.vc")
ENDIF()

CONFIGURE_FILE(${jconfig} jconfig.h @ONLY)
CONFIGURE_FILE(${LIBJPEG_SOURCE_DIR}/jerror.h jerror.h COPYONLY)
CONFIGURE_FILE(${LIBJPEG_SOURCE_DIR}/jmorecfg.h jmorecfg.h COPYONLY)
CONFIGURE_FILE(${LIBJPEG_SOURCE_DIR}/jpeglib.h jpeglib.h COPYONLY)

SET(LIBJPEG_PUBLIC_HEADERS
  ${CMAKE_CURRENT_BINARY_DIR}/jconfig.h
  ${CMAKE_CURRENT_BINARY_DIR}/jerror.h
  ${CMAKE_CURRENT_BINARY_DIR}/jmorecfg.h
  ${CMAKE_CURRENT_BINARY_DIR}/jpeglib.h
)

SET(LIBJPEG_PRIVATE_HEADERS
  ${LIBJPEG_SOURCE_DIR}/jdct.h
  ${LIBJPEG_SOURCE_DIR}/jinclude.h
  ${LIBJPEG_SOURCE_DIR}/jmemsys.h
  ${LIBJPEG_SOURCE_DIR}/jpegint.h
  ${LIBJPEG_SOURCE_DIR}/jversion.h
)

SET(LIBJPEG_SRCS
  ${LIBJPEG_SOURCE_DIR}/jaricom.c
  ${LIBJPEG_SOURCE_DIR}/jcapimin.c
  ${LIBJPEG_SOURCE_DIR}/jcapistd.c
  ${LIBJPEG_SOURCE_DIR}/jcarith.c
  ${LIBJPEG_SOURCE_DIR}/jccoefct.c
  ${LIBJPEG_SOURCE_DIR}/jccolor.c
  ${LIBJPEG_SOURCE_DIR}/jcdctmgr.c
  ${LIBJPEG_SOURCE_DIR}/jchuff.c
  ${LIBJPEG_SOURCE_DIR}/jcinit.c
  ${LIBJPEG_SOURCE_DIR}/jcmainct.c
  ${LIBJPEG_SOURCE_DIR}/jcmarker.c
  ${LIBJPEG_SOURCE_DIR}/jcmaster.c
  ${LIBJPEG_SOURCE_DIR}/jcomapi.c
  ${LIBJPEG_SOURCE_DIR}/jcparam.c
  ${LIBJPEG_SOURCE_DIR}/jcprepct.c
  ${LIBJPEG_SOURCE_DIR}/jcsample.c
  ${LIBJPEG_SOURCE_DIR}/jctrans.c
  ${LIBJPEG_SOURCE_DIR}/jdapimin.c
  ${LIBJPEG_SOURCE_DIR}/jdapistd.c
  ${LIBJPEG_SOURCE_DIR}/jdarith.c
  ${LIBJPEG_SOURCE_DIR}/jdatadst.c
  ${LIBJPEG_SOURCE_DIR}/jdatasrc.c
  ${LIBJPEG_SOURCE_DIR}/jdcoefct.c
  ${LIBJPEG_SOURCE_DIR}/jdcolor.c
  ${LIBJPEG_SOURCE_DIR}/jddctmgr.c
  ${LIBJPEG_SOURCE_DIR}/jdhuff.c
  ${LIBJPEG_SOURCE_DIR}/jdinput.c
  ${LIBJPEG_SOURCE_DIR}/jdmainct.c
  ${LIBJPEG_SOURCE_DIR}/jdmarker.c
  ${LIBJPEG_SOURCE_DIR}/jdmaster.c
  ${LIBJPEG_SOURCE_DIR}/jdmerge.c
  ${LIBJPEG_SOURCE_DIR}/jdpostct.c
  ${LIBJPEG_SOURCE_DIR}/jdsample.c
  ${LIBJPEG_SOURCE_DIR}/jdtrans.c
  ${LIBJPEG_SOURCE_DIR}/jerror.c
  ${LIBJPEG_SOURCE_DIR}/jfdctflt.c
  ${LIBJPEG_SOURCE_DIR}/jfdctfst.c
  ${LIBJPEG_SOURCE_DIR}/jfdctint.c
  ${LIBJPEG_SOURCE_DIR}/jidctflt.c
  ${LIBJPEG_SOURCE_DIR}/jidctfst.c
  ${LIBJPEG_SOURCE_DIR}/jidctint.c
  ${LIBJPEG_SOURCE_DIR}/jmemmgr.c
  ${LIBJPEG_SOURCE_DIR}/jmemnobs.c
  ${LIBJPEG_SOURCE_DIR}/jquant1.c
  ${LIBJPEG_SOURCE_DIR}/jquant2.c
  ${LIBJPEG_SOURCE_DIR}/jutils.c
)

# Dependent include files
INCLUDE_DIRECTORIES(${LIBJPEG_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

IF(MSVC)
  LIST(APPEND PRIVATE_DEFINITIONS -D_CRT_SECURE_NO_WARNINGS) # Disable warnings for unsecure CRT functions
  LIST(APPEND PRIVATE_DEFINITIONS -D_LIB)
  LIST(APPEND PRIVATE_OPTIONS /Gy) # Eliminate Duplicate Strings
  SET(CMAKE_DEBUG_POSTFIX "d")
ENDIF(MSVC)

ADD_LIBRARY(jpeg STATIC ${LIBJPEG_SRCS} ${LIBJPEG_PRIVATE_HEADERS} ${LIBJPEG_PUBLIC_HEADERS})

# Dependent include files
TARGET_INCLUDE_DIRECTORIES(jpeg PUBLIC 
  $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> 
  $<INSTALL_INTERFACE:include>
  )

TARGET_COMPILE_DEFINITIONS(jpeg
  PRIVATE ${PRIVATE_DEFINITIONS}
  )
  
TARGET_COMPILE_OPTIONS(jpeg 
  PRIVATE ${PRIVATE_OPTIONS}
  )


# INSTALL
INSTALL(TARGETS jpeg EXPORT jpeg_export
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  )

INSTALL(FILES 
  ${LIBJPEG_PUBLIC_HEADERS} 
  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  )

